package cn.com.archie.mavneup;

import lombok.extern.slf4j.Slf4j;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.FileWriter;


/**
 * @author linaq
 * @date 2024-05-29
 */
@Slf4j
public class MavenJarUp {
    public static String localRepoAbsPath = "E:\\chinatelecom";
    public static String commands = "commands.txt";
    //远程仓库的上传路径 根据路径区分不同的环境（snapshots，releases）
    public static String commandTemplate = "mvn deploy:deploy-file %s %s -DgroupId=%s -DartifactId=%s -Dversion=%s -Dpackaging=%s -Durl=http://134.96.219.165:8081/repository/maven-snapshots/ -DrepositoryId=maven-snapshots";
    public static int jarFileCount = 0;
    public static int pomFileCount = 0;

    public static void main(String[] args) {
        File directory = new File(localRepoAbsPath);
        FileWriter writer = null;

        try {
            writer = new FileWriter(commands);

            processDirectory(directory, writer);

            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void processDirectory(File directory, FileWriter writer) throws Exception {

        for (File file : directory.listFiles()) {
            if (file.isDirectory()) {
                processDirectory(file, writer);
            } else if (file.getName().endsWith(".pom")) {
                if (commandTemplate.contains("snapshots")) {
                    if (!file.getName().contains("SNAPSHOT")) {
                        continue;
                    }
                } else {
                    if (file.getParentFile().getName().contains("SNAPSHOT")) {
                        continue;
                    }
                }
                processPomFileLog(file, writer);
            }
        }
    }


    public static void processPomFileLog(File pomFile, FileWriter writer) throws Exception {
        String command = null;
        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(pomFile);
            String pomPath = pomFile.getAbsoluteFile().getAbsolutePath();
            doc.getDocumentElement().normalize();
            SAXReader saxReader = new SAXReader();
            org.dom4j.Document document = saxReader.read(pomFile);
            Element rootElement = document.getRootElement();
            String groupId = rootElement.elementText("groupId");
            String artifactId = rootElement.elementText("artifactId");
            String version = rootElement.elementText("version");
            File jarFile = new File(pomFile.getAbsolutePath().replace(".pom", ".jar"));

            String fileParam = jarFile.exists() ? "-Dfile=" + jarFile.getAbsolutePath() : "-Dfile=" + pomPath;
            String pomParam = jarFile.exists() ? "-DpomFile=" + pomPath : "";
            String packaging = jarFile.exists() ? "jar" : "pom";

            if (jarFile.exists()) {
                jarFileCount++;
                log.info("Jar file count: " + jarFileCount);
            } else {
                pomFileCount++;
                log.info("only  Pom file count: " + pomFileCount);
            }

            command = String.format(commandTemplate,
                    fileParam, pomParam, groupId, artifactId, version, packaging);

            writer.write(command + "\n");
        } catch (Exception e) {
            log.error("err command:{}", command);
        }
    }
}
